import 'package:dailoz/data/page_data.dart';
import 'package:dailoz/generated/l10n.dart';
import 'package:dailoz/service/language_service/language_service.dart';
import 'package:dailoz/size_config.dart';
import 'package:flutter/cupertino.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

class ShowDialog {
  static void showAlertDialog(
      BuildContext context, String content, VoidCallback confirmFunction) {
    showCupertinoModalPopup(
      context: context,
      builder: (BuildContext context) => CupertinoAlertDialog(
        content: Text(
          content,
          style: TextStyle(
            fontSize: SizeConfig.getProportionateFontSize(14.0),
          ),
        ),
        actions: <CupertinoDialogAction>[
          CupertinoDialogAction(
            isDefaultAction: true,
            onPressed: () {
              Navigator.pop(context);
            },
            child: Text(S.of(context).no),
          ),
          CupertinoDialogAction(
            isDestructiveAction: true,
            onPressed: confirmFunction,
            child: Text(S.of(context).yes),
          ),
        ],
      ),
    );
  }

  static void showDialog(BuildContext context, Widget child) {
    showCupertinoModalPopup<void>(
      context: context,
      builder: (BuildContext context) => Container(
        height: SizeConfig.getProportionateScreenHeight(216.0),
        padding: const EdgeInsets.only(top: 6.0),
        margin: EdgeInsets.only(
          bottom: MediaQuery.of(context).viewInsets.bottom,
        ),
        color: CupertinoColors.systemBackground.resolveFrom(context),
        child: SafeArea(
          top: false,
          child: child,
        ),
      ),
    );
  }

  static void showActionSheet(BuildContext context, void Function() getName) {
    final provider = Provider.of<PageData>(context, listen: false);
    showCupertinoModalPopup(
      context: context,
      builder: (BuildContext context) => CupertinoActionSheet(
        title: const Text("选择语言"),
        actions: [
          CupertinoActionSheetAction(
            onPressed: () {
              S.load(const Locale("zh", "CN"));
              Navigator.pop(context);
              LanguageService.setLanguage("zh", "CN");
              LanguageService.setLanguageName("中文");
              getName();
            },
            child: const Text("中文"),
          ),
          CupertinoActionSheetAction(
            onPressed: () {
              S.load(const Locale("en", "US"));
              Navigator.pop(context);
              LanguageService.setLanguage("en", "US");
              LanguageService.setLanguageName("English(US)");
              getName();
            },
            child: const Text("English"),
          )
        ],
      ),
    );
  }
}
